﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Habanero.Base;
using Habanero.BO;

namespace FireStarterModeller.Logic
{
    public class MemoryDumpUtilities
    {
        public string DumpInMemoryDataStore(DataStoreInMemory dataStoreInMemory)
        {
            if (dataStoreInMemory == null) return "<DataStoreInMemory/>";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(string.Format("<DataStoreInMemory Count='{0}'>", dataStoreInMemory.Count));
            foreach (KeyValuePair<Guid, IBusinessObject> keyValuePair in dataStoreInMemory.AllObjects)
            {
                stringBuilder.AppendLine(DumpBusinessObject(keyValuePair.Value));
            }
            return stringBuilder.ToString() + "</DataStoreInMemory>";
        }
        public string DumpBusinessObject(IBusinessObject businessObject)
        {
            if (businessObject == null) return "<BusinessObject/>";
            StringBuilder stringBuilder = new StringBuilder();
            string xmlElementName = string.Format("BusinessObject_{0}", businessObject.ClassDef.ClassName);
            stringBuilder.AppendLine(string.Format("<{0} ID='{1}'>", xmlElementName, businessObject.ID.ToString()));
            stringBuilder.AppendLine(DumpBOStatus(businessObject.Status));
            foreach (IBOProp boProp in businessObject.Props)
            {
                stringBuilder.AppendLine(DumpBOProp(boProp));
            }
            return stringBuilder.ToString() + string.Format("</{0}>", xmlElementName);
        }

        public string DumpBOStatus(IBOStatus boStatus)
        {
            if (boStatus == null) return "<BOStatus/>";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(string.Format("<BOStatus "));
            stringBuilder.Append(string.Format("IsNew='{0}' ", boStatus.IsNew));
            stringBuilder.Append(string.Format("IsDeleted='{0}' ", boStatus.IsDeleted));
            stringBuilder.Append(string.Format("IsDirty='{0}' ", boStatus.IsDirty));
            stringBuilder.Append(string.Format("IsEditing='{0}' ", boStatus.IsEditing));
            //stringBuilder.Append(string.Format("IsValid={0},", boStatus.IsValid()));
            //stringBuilder.Append(string.Format("IsValidMessage='{0}'", boStatus.IsValidMessage));
            stringBuilder.Append(string.Format("/>"));
            return stringBuilder.ToString();
        }

        public string DumpBOProp(IBOProp boProp)
        {
            if (boProp == null) return "<BOProp/>";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(string.Format("<BOProp "));
            stringBuilder.Append(string.Format("Name='{0}' ", boProp.PropertyName));
            stringBuilder.Append(string.Format("IsDirty='{0}' ", boProp.IsDirty));
            stringBuilder.Append(string.Format("PropertyValue='{0}' ", boProp.PropertyValueString));
            stringBuilder.Append(string.Format("PersistedPropertyValue='{0}' ", boProp.PersistedPropertyValueString));
            stringBuilder.Append(string.Format("/>"));
            return stringBuilder.ToString();
        }
    }
}
